package com.dayou.service.impl;

import java.util.*;

import com.dayou.bean.InterviewerRecord;
import com.dayou.bean.MqMessageBean;
import com.dayou.bean.RegInfo;
import com.dayou.mapper.InterviewMapper;
import com.dayou.service.MqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.dayou.mapper.RoomMapper;
import com.dayou.bean.Room;
import com.dayou.service.IRoomService;

/**
 * 房间 服务层实现
 * 
 * @author dayou
 * @date 2020-04-29
 */
@Service
public class RoomService implements IRoomService
{
	@Autowired
	private RoomMapper roomMapper;
	@Autowired
	private InterviewMapper interviewMapper;
	@Autowired
	private RegInfoService regInfoService;
	@Autowired
	private MqService mqService;
	@Value("${interview.module-name}")
	private String moduleName;

	/**
     * 查询房间信息
     * 
     * @param ardId 房间ID
     * @return 房间信息
     */
    @Override
	public Room selectRoomById(Integer ardId)
	{
	    return roomMapper.selectRoomById(ardId);
	}
	
	/**
     * 查询房间列表
     * 
     * @param room 房间信息
     * @return 房间集合
     */
	@Override
	public List<Room> selectRoomList(Room room)
	{
	    return roomMapper.selectRoomList(room);
	}
	
	/**
     * 修改房间
     * 
     * @param room 房间信息
     * @return 结果
     */
	@Override
	public int updateRoom(Room room)
	{
		boolean mqFlag = true;
		if(!new Integer(1).equals(room.getState())){
			Integer roomId = room.getArdId();
			Room roomInfo = selectRoomById(room.getArdId());
			//查询正在等待中的提审会见记录
			InterviewerRecord waitingCondition = new InterviewerRecord();
			waitingCondition.setParams(new HashMap<String, Object>() {
				{
					put("waiting", true);
				}
			});
			boolean freeFlag = true;
			List<InterviewerRecord> waitingRecords = interviewMapper.list(waitingCondition);
			//遍历等待中的记录，当发现与当前结束记录同类型时，将房间分配给该记录并推送语音消息
			for (InterviewerRecord r : waitingRecords) {
				Set<Integer> roomInterviewTypes = new HashSet<>();
				switch (roomInfo.getType()){
					case 1:
						roomInterviewTypes.add(0);
					case 2:
						roomInterviewTypes.add(1);
					case 3:
						roomInterviewTypes.add(0);
					case 4:
						roomInterviewTypes.add(0);
						roomInterviewTypes.add(1);
					case 5:
						roomInterviewTypes.add(0);
				}
				Integer type = r.getType();
				if (roomInterviewTypes.contains(type)) {
					freeFlag = false;
					mqFlag = false;
					r.setRoomId(roomId);
					interviewMapper.updateInterviewerRoomId(r);
					interviewMapper.updateInterviewerDtlsRoomId(r);
					RegInfo regInfo = regInfoService.selectRegInfoById(r.getVisitorId());
					String name = regInfo.getName();
					String roomNo = selectRoomById(roomId).getNumber();
					String typeStr = type == 0 ? "提审" : "会见";
					String voiceStr = String.format("请%s到%s房间进行%s", name, roomNo, typeStr);
					mqService.sendMessage(new MqMessageBean("room", "room-voice", voiceStr, moduleName));
					room.setVisitorId(r.getVisitorId());
					break;
				}
			}
			if (freeFlag) {
				room.setState(0);
			}
		}
		if(mqFlag){
			mqService.sendMessage(new MqMessageBean("room", "room-update", "", moduleName));
			mqService.sendMessage(new MqMessageBean("selfHelp", "room-update", "", moduleName));
		}
		return roomMapper.updateRoom(room);
	}
}
